/**
 * 
 */
package org.bookie.stateful;

import org.apache.hivemind.service.ThreadCleanupListener;

class StatefulService implements ThreadCleanupListener, 
										 StateStorageClearanceListener
{
    /**
	 * 
	 */
	private final StatefulServiceModel statefulServiceModel;

	private Object _core;

    private StatefulServiceLifecycleListener _managed;
    
    

    /**
     * @param core
     *            the core service implementation, which may optionally implement
     *            {@link StatefulServiceLifecycleListener}
     * @param statefulServiceModel TODO RZ
     */
    StatefulService(StatefulServiceModel statefulServiceModel, Object core )
    {
    		
        this.statefulServiceModel = statefulServiceModel;
		_core = core;

        if (core instanceof StatefulServiceLifecycleListener)
            _managed = (StatefulServiceLifecycleListener) core;
        else
            _managed = StatefulServiceModel.NULL_MANAGEABLE;
    }

    public void threadDidCleanup()
    {
        this.statefulServiceModel.unbindPooledServiceFromCurrentThread(this);
    }

    void activate()
    {
        _managed.resumeConversation();
    }

    void passivate()
    {
        _managed.pauseConversation();
    }
    
    public void clientStateCleared() 
    {
        _managed.terminateConversation();
    }
    /**
     * Returns the configured service implementation.
     */
    public Object getService()
    {
        return _core;
    }

}